package com.parkingspace.controller;


import com.parkingspace.dao.UserMapper;
import com.parkingspace.entity.OwnerQuery;
import com.parkingspace.entity.PropertyQuery;
import com.parkingspace.entity.RenterQuery;
import com.parkingspace.entity.User;
import com.parkingspace.service.IUserService;
import com.parkingspace.service.impl.UserServiceImpl;
import com.parkingspace.util.Constants;
import com.parkingspace.util.ResultVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/user")
public class UserController {

	@Resource
	private IUserService iUserService;

	@Resource
	private RedisTemplate<String, User> redisTemplate;

	@Resource
	private UserMapper userMapper;


	/**
	 * 进行登录验证操作
	 *
	 * @param uaccount 传入账号
	 * @param upass    传入密码
	 * @return 返回ResultVO
	 */
	@RequestMapping("/login")
	public ResultVO login(String uaccount, String upass) {
		if (uaccount == null || upass == null) {
			return new ResultVO(Constants.OPER_FAILURE, "账号或密码不能为空!", false);
		}
		try {
			Map<String, Object> some = iUserService.findSome(uaccount, upass);
			return new ResultVO(Constants.OPER_SUCCESS, "登录成功", some);
		} catch (Exception e) {
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "账号或密码不正确");
		}
	}

	@GetMapping("/registered")
	public ResultVO register(User user) {
		ResultVO resultVO;
		Integer row = iUserService.saveUser(user);
		if (row == 0) {
			resultVO = new ResultVO(Constants.OPER_FAILURE, "失败", false);
			return resultVO;
		} else if (row == -1) {
			resultVO = new ResultVO(Constants.OPER_FAILURE, "该昵称已被占用", false);
			return resultVO;
		}
		resultVO = new ResultVO(Constants.OPER_SUCCESS, "成功", true);
		return resultVO;
	}

	/**
	 * 进行用户选择进入界面的信息存储
	 *
	 * @param token 登录成功的令牌
	 * @param id    2代表业主,3代表租客
	 * @return 返回ResultVO, data存储了boolean
	 */
	@PostMapping("/getUserNum/{token}/{id}")
	public ResultVO getUserNum(@PathVariable("token") String token, @PathVariable("id") Integer id) {
		try {
			iUserService.getUserNum(token, id);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), Boolean.FALSE);
		}
		return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", Boolean.TRUE);
	}

	/**
	 * 进行实名认证
	 *
	 * @param token  传入令牌
	 * @param uname  传入真实姓名
	 * @param idCard 身份证号
	 * @return 返回0:认证失败!,1:认证成功!
	 */
	@PutMapping("/updateVerified/{token}")
	public ResultVO updateVerified(@PathVariable("token") String token, String uname, String idCard) {
		try {
			iUserService.updateVerified(token, uname, idCard);
			return new ResultVO(Constants.OPER_SUCCESS, "实名认证成功!", 1);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), 0);
		}
	}

	/**
	 * 进行实名认证检测
	 *
	 * @param token 传入token
	 * @return 返回数字 0:未实名,1:已实名
	 */
	@GetMapping("/realNameDetection/{token}")
	public ResultVO realNameDetection(@PathVariable("token") String token) {
		try {
			Integer i = iUserService.realNameDetection(token);
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", i);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "请求失败!");
		}
	}

	/**
	 * 查询user表中的所有信息
	 *
	 * @return 返回List
	 */
	@GetMapping("/findAll")
	public ResultVO findAll() {
		try {
			//将User表中的所有信息存入redis
			List<User> userall = redisTemplate.opsForList().range("userall", 0, -1);
			if (userall.size() == 0) {
				synchronized (UserServiceImpl.class) {
					if (userall.size() == 0) {
						List<User> users = userMapper.selectList(null);
						redisTemplate.opsForList().rightPushAll("userall", users);
						redisTemplate.expire("userall", 5, TimeUnit.MINUTES);
						userall = redisTemplate.opsForList().range("userall", 0, -1);
					}
				}
			}
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", userall);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "请求失败!");
		}
	}

	/**
	 * 查询用户的个人信息并查询此人名下有多少个车位
	 *
	 * @param token 传入token
	 * @return 返回User
	 */
	@GetMapping("/findByName/{token}")
	public ResultVO findByName(@PathVariable("token") String token) {
		try {
			Map<String, Object> user = iUserService.findByName(token);
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", user);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "请求失败!");
		}
	}

	/**
	 * 修改密码
	 *
	 * @param token       传入令牌
	 * @param oldPassword 旧密码
	 * @param newPassword 新密码
	 * @return 返回Boolean
	 */
	@PutMapping("/updateUpass/{token}")
	public ResultVO updateUpass(@PathVariable("token") String token, String oldPassword, String newPassword) {
		try {
			Boolean aBoolean = iUserService.updateUpass(token, oldPassword, newPassword);
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", aBoolean);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "请求失败!");
		}
	}

	/**
	 * 修改用户信息
	 *
	 * @param token 传入令牌
	 * @param user  传入要修改的用户信息
	 * @return 返回Boolean
	 */
	@PutMapping("/updateUser/{token}")
	public ResultVO updateUser(@PathVariable("token") String token, User user) {
		try {
			Boolean aBoolean = iUserService.updateUser(token, user);
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", aBoolean);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage(), "请求失败!");
		}
	}

	/**
	 * 软删除用户
	 *
	 * @param uid
	 * @return
	 */
	@RequestMapping("/del")
	public ResultVO del(Long uid) {
		boolean f = iUserService.delete(uid);
		return new ResultVO(Constants.OPER_SUCCESS,"删除成功");
	}

	@RequestMapping("/findBy")
	public ResultVO findBy(User user) {

		if ((user.getUname().trim().equals("") || user.getUname() == null) && (user.getUidcard().trim().equals("") || user.getUidcard() == null)) {
			List<User> all = iUserService.findAll();
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功!", all);
		}
		System.out.println("---------------" + user.getUidcard() + user.getUname());
		List<User> userList = iUserService.findBy(user);
		return new ResultVO(Constants.OPER_SUCCESS, "请求成功", userList);
	}


	/**
	 * 租客用户的查询
	 *
	 * @param user
	 * @return
	 */
	@RequestMapping("/renterQuery")
	public ResultVO renterQuery(User user) {
		List<User> userList = iUserService.renterAll(user);
		return new ResultVO(Constants.OPER_SUCCESS, "查询成功", userList);
	}

	/**
	 * 业主的查询
	 *
	 * @param user
	 * @return
	 */
	@RequestMapping("/ownerQuery")
	public ResultVO ownerQuery(User user) {
		List<User> userList = iUserService.ownerAll(user);
		return new ResultVO(Constants.OPER_SUCCESS, "查询成功", userList);
	}

	/**
	 * 物业的查询
	 *
	 * @param uname
	 * @param propertyName
	 * @return
	 */
	@RequestMapping("/propertyQuery")
	public ResultVO propertyQuery(String uname, String propertyName) {
		List<PropertyQuery> propertyQueryList = iUserService.propertyAll(uname, propertyName);
		return new ResultVO(Constants.OPER_SUCCESS, "查询成功", propertyQueryList);
	}

	/**
	 * 租客的详细信息
	 *
	 * @param uid
	 * @return
	 */
	@RequestMapping("/renterDetail")
	public ResultVO renterDetail(Long uid) {
		RenterQuery renterQuery = iUserService.renterDetail(uid);
		return new ResultVO(Constants.OPER_SUCCESS, "查询成功", renterQuery);
	}

	/**
	 * 业主的详细信息
	 *
	 * @param uid
	 * @return
	 */
	@RequestMapping("/ownerDetail")
	public ResultVO ownerDetail(Long uid) {
		OwnerQuery ownerQuery = iUserService.ownerDetail(uid);
		return new ResultVO(Constants.OPER_SUCCESS, "请求成功", ownerQuery);
	}

	/**
	 * 物业的详细信息
	 *
	 * @param uid
	 * @return
	 */
	@RequestMapping("/propertyDetail")
	public ResultVO propertyDetail(Long uid) {
		PropertyQuery propertyQuery = iUserService.propertyDetail(uid);
		return new ResultVO(Constants.OPER_SUCCESS, "请求成功", propertyQuery);
	}

	/**
	 * 注销登录
	 *
	 * @param token 传入令牌
	 * @return 返回Boolean
	 * @throws Exception
	 */
	@DeleteMapping("/deleteUser/{token}")
	public ResultVO deleteUser(@PathVariable("token") String token) {
		try {
			Boolean aBoolean = iUserService.deleteUser(token);
			return new ResultVO(Constants.OPER_SUCCESS, "请求成功", aBoolean);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResultVO(Constants.OPER_FAILURE, e.getMessage());
		}
	}
}

